package CreativePattern.SingletonPattern.DoubleLocking;

//双重加锁
public class Singleton {
    private Singleton() {
    }
    //需要在 singleton 变量上添加 volatile 关键字，确保所有线程都能看到正确的实例，并且确保实例在创建时是完全初始化的。
    public static volatile Singleton singleton;

    public static Singleton getInstance() {

        if (singleton == null) {
            synchronized (Singleton.class) {
                if(singleton == null){
                    singleton = new  Singleton();
                }
            }
        }
        return singleton;
    }
}
